//
//  ZJGetMoneyVC.m
//  这家测试项目
//
//  Created by IOS_DEVELOPER on 2017/7/10.
//  Copyright © 2017年 陈志凌. All rights reserved.
//

#import "ZJGetMoneyVC.h"
#import "ZJBankAccountVC.h"

@interface ZJGetMoneyVC ()
@property (weak, nonatomic) IBOutlet UIButton *bankBtn;
@property (weak, nonatomic) IBOutlet UITextField *moneyTX;
@property (weak, nonatomic) IBOutlet UILabel *blance;
@property (weak, nonatomic) IBOutlet UIButton *getBtn;
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UIButton *allGetBtn;
@property (strong, nonatomic) ZJUserModel * model;
@property (strong, nonatomic) ZJProjectDetailModel * prmodel;
@end

@implementation ZJGetMoneyVC

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self initData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

-(void)initData{
    self.bankBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.getBtn setBackgroundColor:[Utils colorWithHexString:allConstHexColor]];
    self.model = GET_USERINFO;
    self.prmodel = GET_PROJECTINFO;
    [self.moneyTX setValue:[Utils colorWithHexString:@"e7e7e7"] forKeyPath:@"_placeholderLabel.textColor"];
    self.moneyTX.placeholder = [NSString stringWithFormat:@"最低提现 ¥ %.2f",[self.prmodel.withdrawals_limit floatValue]];
    if ([self.model.bank isEqualToString:@""] || [self.model.account isEqualToString:@""]) {
        [self.bankBtn setTitle:@"请选择银行卡" forState:0];
    }else{
        NSString *num = [self.model.account substringFromIndex:(self.model.account.length - 4)];
        [self.bankBtn setTitle:[NSString stringWithFormat:@"%@(%@)",self.model.bank,num] forState:0];
    }
    
    self.blance.text = [NSString stringWithFormat:@"账户余额 ¥ %@",self.model.money_total];
    CGSize titleSize = [self.blance.text sizeWithFont:self.blance.font constrainedToSize:CGSizeMake(MAXFLOAT, 30)];
    self.blance.frame = CGRectMake(8, 191, titleSize.width, 21);
    self.allGetBtn.frame = CGRectMake(self.blance.width + 8, 191, 80, 21);
}

- (IBAction)changeBtn:(UIButton *)sender {
    ZJBankAccountVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJBankAccountVC"];

    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)allGetMoney:(UIButton *)sender {
    self.moneyTX.text = self.model.money_total;
}
- (IBAction)getBtn:(UIButton *)sender {
    
    if ([self.moneyTX.text floatValue] <= 0) {
        [HUD showMsg:@"提现金额不能为0"];
        return;
    }else if ([self.bankBtn.titleLabel.text isEqualToString:@"请选择银行卡"]){
        [HUD showMsg:@"请选择银行卡"];
        return;
    }else if ([self.moneyTX.text floatValue] < [self.prmodel.withdrawals_limit floatValue]){
        [HUD showMsg:[NSString stringWithFormat:@"提现金额不得低于:%.0f",[self.prmodel.withdrawals_limit floatValue]]];
        return;
    }
    
    [HUD showLoading:@"提交中..."];
    NSDictionary *parameter = [LRNetwork requestParameter:@{@"money":self.moneyTX.text
                                                           }];
    NSString *url = [LRNetwork requestURL:@"cgi/withdrawals.php"];
    
    [LRNetwork netRequestPOSTWithRequestURL:url withParameter:parameter withReturnValeuBlock:^(id returnValue) {
        
        if ([self.moneyTX.text floatValue] == [self.model.money_total floatValue]) {
            self.model.money_total = @"0";
            self.model.money_commission = @"0";
            self.model.money_mission = @"0";
            self.model.money_package = @"0";
            SYN_USER(self.model);
            
        }else{
            self.model.money_total = [NSString stringWithFormat:@"%.2f",[self.model.money_total floatValue] - [self.moneyTX.text floatValue]];
            SYN_USER(self.model);
        }
        [HUD showSucceedMsg:@"申请成功"];
        [self.navigationController popViewControllerAnimated:YES];
        
    } withErrorCodeBlock:^(NSInteger errorCode, NSString *errorMessage) {
        [HUD showErrorMsg:errorMessage];
    } withFailureBlock:^{
        
    }];
}

@end
